﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DAL.Product;

namespace ProductStoreV4.Api.Controllers
{
    /// <summary>
    ///
    /// </summary>
    public class ProductController : ApiController
    {
        private ProductRepository _repository = new ProductRepository();

        /// <summary>
        /// Gets this instance.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<BO.Product> Get()
        {
            return _repository.GetAll();
        }

        /// <summary>
        /// Gets the specified id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public BO.Product Get(int id)
        {
            if (id < 1)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            var product = _repository.GetById(id);
            if (product == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }
            return product;
        }

        /// <summary>
        /// Posts the specified value.
        /// </summary>
        /// <param name="value">The value.</param>
        public HttpResponseMessage Insert(BO.Product product)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            _repository.Insert(product);

            var response = new HttpResponseMessage(HttpStatusCode.Created);
            response.Headers.Location = new Uri("/Get/" + product.ProductId.ToString());
            return response;
        }

        /// <summary>
        /// Puts the specified product.
        /// </summary>
        /// <param name="product">The product.</param>
        public void Put(BO.Product product)
        {
        }

        /// <summary>
        /// Deletes the specified id.
        /// </summary>
        /// <param name="id">The id.</param>
        public int Delete(int id)
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
            //return _repository.Delete(id);
        }
    }
}